package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.domain.ClientTest;
import cn.wolfcode.education.query.ClienttestQuery;
import cn.wolfcode.education.query.SchoolQuery;
import cn.wolfcode.education.service.IClientTestService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("/clienttest")
public class ClientTestController {
    @Autowired
    private IClientTestService clientTestService;

    @RequestMapping("")
    public String index(){
        return "client/clienttest";
    }
    @ResponseBody
    @RequestMapping("/list")
    public PageResult list(ClienttestQuery qo) {
        return clientTestService.queryForList(qo);
    }
    @ResponseBody
    @RequestMapping("/save")
    public JSONResult save(ClientTest clientTest){
        JSONResult jsonResult = new JSONResult();
        try{
            clientTestService.insertTest(clientTest);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }

    /**
     * 编辑考试成绩
     * @param clientTest
     * @return
     */
    @ResponseBody
    @RequestMapping("/savegrade")
    public JSONResult savegrade(ClientTest clientTest){
        JSONResult jsonResult = new JSONResult();
        try{
            clientTestService.savegrade(clientTest);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }

    /**
     * 删除考试记录
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public JSONResult delete(Long id){
        JSONResult jsonResult = new JSONResult();
        try{
            clientTestService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg("系统异常");
        }
        return jsonResult;
    }
    @RequestMapping("/download")
    @ResponseBody
    public void download(SchoolQuery qo, HttpServletResponse resp) throws Exception{
        qo.setRows(100);
        List<ClientTest> rows = clientTestService.queryForList(qo).getRows();

        resp.setHeader("Content-Disposition","attachment;filename=Clienttest.xls");
        OutputStream outputStream = resp.getOutputStream();

        //创建工作簿
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);

        //处理标题
        //创建sheet
        WritableSheet sheet = workbook.createSheet("Clienttest", 0);
        //创建单元格
        Label idLabel = new Label(0, 0, "编号");
        Label nameLabel = new Label(1, 0, "姓名");
        Label saleLabel = new Label(2, 0, "营销人员");
        Label qqLabel = new Label(3, 0, "QQ");
        Label telLabel = new Label(4, 0, "电话");
        Label washClassLabel = new Label(5, 0, "意向班级");
        Label testTimeLabel = new Label(6, 0, "考试时间");
        Label testTypeLabel = new Label(7, 0, "考试类型");
        Label testGradeLabel = new Label(8, 0, "考试成绩");
        Label resultLabel = new Label(9, 0, "考试结果");
        Label rearkLabel = new Label(10, 0, "备注");
        Label handlerLabel = new Label(11, 0, "处理人");

        sheet.addCell(idLabel);
        sheet.addCell(nameLabel);
        sheet.addCell(saleLabel);
        sheet.addCell(qqLabel);
        sheet.addCell(telLabel);
        sheet.addCell(washClassLabel);
        sheet.addCell(testTimeLabel);
        sheet.addCell(testTypeLabel);
        sheet.addCell(testGradeLabel);
        sheet.addCell(resultLabel);
        sheet.addCell(rearkLabel);
        sheet.addCell(handlerLabel);

        for (int i = 0; i < rows.size(); i ++){
            ClientTest s = rows.get(i);

            int row = i + 1;

            Label sidLabel = new Label(0, row, s.getId().toString());
            Label snameLabel = new Label(1, row, s.getClient().getName());
            Label ssaleLabel = new Label(2, row, s.getClient().getInputEmployee() != null ? s.getClient().getInputEmployee().getRealName() :null);
            Label sqqLabel = new Label(3, row, s.getClient().getQq().toString());
            Label stelLabel = new Label(4, row, s.getClient().getTel().toString());
            Label swashClassLabel = new Label(5, row, s.getClient().getWishClass().getName());
            Label stestTimeLabel = new Label(6, row, s.getTestTime() != null? s.getTestTime().toLocaleString():null);
            Label stestTypeLabel = new Label(7, row, s.getTestType().getName());
            Label stestGradeLabel = new Label(8, row, s.getTestGrade()!=null ? s.getTestGrade().toString():null);
            Label sresultLabel = new Label(9, row, s.getTestStateString());
            Label srearkLabel = new Label(10, row, s.getRemark() != null ? s.getRemark() : null);
            Label shandlerLabel = new Label(11, row, s.getHandler() != null ?s.getHandler().getRealName() : null);

            sheet.addCell(sidLabel);
            sheet.addCell(snameLabel);
            sheet.addCell(ssaleLabel);
            sheet.addCell(sqqLabel);
            sheet.addCell(stelLabel);
            sheet.addCell(swashClassLabel);
            sheet.addCell(stestTimeLabel);
            sheet.addCell(stestTypeLabel);
            sheet.addCell(stestGradeLabel);
            sheet.addCell(sresultLabel);
            sheet.addCell(srearkLabel);
            sheet.addCell(shandlerLabel);
        }
        workbook.write();
        workbook.close();
    }
}
